package com.xmsit.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public HttpMessageConverter fastJsonHttpMessageConverter() {
        //创建FastJsonHttpMessageConverter对象，并设置FastJsonConfig配置
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //创建FastJsonConfig对象，并设置序列化特性
        FastJsonConfig config = new FastJsonConfig();
        //设置序列化特性
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //设置日期格式
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置全局序列化配置，将Long类型转换为String类型
        SerializeConfig.globalInstance.put(Long.class, ToStringSerializer.instance);
        //设置FastJsonConfig对象
        converter.setFastJsonConfig(config);
        //返回HttpMessageConverter对象
        HttpMessageConverter<?> converter1 = converter;
        return converter1;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverter());
    }

}
